using System;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements;
using Object = UnityEngine.Object;

namespace SCore.Editor.WindowDraw
{
    public class EditorDrawHelp
    {
        //Color
        public static Color _white = new Color32(255, 255, 255, 255);
        public static Color _red = new Color32(255, 0, 0, 255);
        public static Color _green = new Color32(0, 255, 0, 255);
        public static Color _blue = new Color32(0, 0, 255, 255);
        public static Color _black = new Color32(51, 51, 51, 255);
        public static Color _gray = new Color32(128, 128, 128, 255);
        public static Color _yellow = new Color32(255, 255, 0, 255);
        public static Color _gold = new Color32(255, 215, 0, 255);


        public static Color _ivory1 = new Color32(255, 255, 240, 255);
        public static Color _ivory3 = new Color32(205, 205, 193, 255);
        public static Color _ivory4 = new Color32(139, 139, 131, 255);
        public static Color _ivory5 = new Color32(41, 36, 33, 255);
        public static Color _ivory6 = new Color32(17, 17, 17, 255);


        public static Color _seethroughRed = new Color32(176, 23, 31, 128);
        public static Color _indianRed = new Color32(176, 23, 31, 255);
        public static Color _crimsonRed = new Color32(176, 23, 31, 255);

        public static Color _magentaPurple = new Color(139, 0, 139, 255);

        public static Color _tanBrown = new Color32(139, 90, 43, 255);

        public static Color _royalBlue = new Color32(65, 105, 225, 255);

        public static Color _seaGreen1 = new Color32(84, 255, 159, 255);
        public static Color _limegreen = new Color32(50, 205, 50, 255);

        public static Color _firebrick1 = new Color32(255, 48, 48, 255);

        public static Color _orange = new Color32(255, 165, 0, 255);

        public static Color pink = new Color32(255, 192, 203, 255);

        public static Color HexToColor(string hex)
        {
            Color color = new Color();

            if (ColorUtility.TryParseHtmlString(hex, out color))
            {
                return color;
            }
            else
            {
                Debug.LogWarning("Invalid color code: " + hex);
                return Color.white; // 默认返回白色或其他适当的默认颜色
            }
        }

        //GUIStyle
        private static GUIStyle timeCursor;

        public static GUIStyle _timeCursor
        {
            get
            {
                if (timeCursor == null)
                {
                    timeCursor = EditorStyles.FromUSS("Icon-TimeCursor");
                }

                return timeCursor;
            }
        }


        private static GUIStyle timeEndCursor;

        public static GUIStyle _timeEndCursor
        {
            get
            {
                if (timeEndCursor == null)
                {
                    timeEndCursor = EditorStyles.FromUSS("Icon-Endmarker");
                }

                return timeEndCursor;
            }
        }


        public static GUIStyle GetTextGUIStyle(int fontSize, TextAnchor textAnchor, Color color,
            FontStyle fontStyle = FontStyle.Normal)
        {
            GUIStyle myStyle = new GUIStyle();
            myStyle.fontSize = fontSize;
            myStyle.normal.textColor = color;
            myStyle.alignment = textAnchor;
            myStyle.fontStyle = fontStyle;
            return myStyle;
        }

        public static GUIStyle GetButtonGUIStyle(int fontSize, TextAnchor textAnchor, Color color)
        {
            GUIStyle myStyle = new GUIStyle(GUI.skin.button);
            myStyle.fontSize = fontSize;
            myStyle.normal.textColor = color;
            myStyle.alignment = textAnchor;
            return myStyle;
        }

        public static GUIStyle GetTitleGUIStyle(Color color)
        {
            GUIStyle myStyle = new GUIStyle();
            myStyle.normal.background = MakeTex(2, 2, color);
            return myStyle;
        }

        public static GUIStyle GetSkinBoxStyle(Color color, int size = 2)
        {
            GUIStyle boxStyle = new GUIStyle(GUI.skin.box);
            boxStyle.normal.background = EditorDrawHelp.MakeTex(2, 2, color); // 背景颜色
            boxStyle.border = new RectOffset(size, size, size, size); // 描边像素宽度
            return boxStyle;
        }

        public static GUIStyle GetBoxStyle(Color color, int size = 2)
        {
            GUIStyle boxStyle = new GUIStyle("box");
            boxStyle.normal.background = EditorDrawHelp.MakeTex(2, 2, color); // 背景颜色
            boxStyle.border = new RectOffset(size, size, size, size); // 描边像素宽度
            return boxStyle;
        }

        public static void GetBorderBoxStyle(Rect rect, Color borderColor, Color backgroundColor, int borderWidth = 2)
        {
            // 创建一个临时的背景色样式
            var backgroundStyle = new GUIStyle(GUI.skin.box);
            backgroundStyle.normal.background = MakeTex(2, 2, backgroundColor);

            // 创建一个临时的边框颜色样式
            var borderStyle = new GUIStyle(GUI.skin.box);
            borderStyle.normal.background = MakeTex(2, 2, borderColor);

            // 绘制边框
            GUILayout.Box("", borderStyle, GUILayout.Width(rect.width), GUILayout.Height(rect.height));

            // 绘制带背景色的内容
            GUILayout.Box("", backgroundStyle);

            // 再次绘制边框作为底部边
            GUILayout.Box("", borderStyle, GUILayout.Width(rect.width), GUILayout.Height(rect.height));
        }

        // 辅助函数用于生成纯色背景
        public static Texture2D MakeTex(int width, int height, Color col)
        {
            Color[] pix = new Color[width * height];
            for (int i = 0; i < pix.Length; i++)
                pix[i] = col;

            Texture2D result = new Texture2D(width, height);
            result.SetPixels(pix);
            result.Apply();
            return result;
        }

        public static string DrawStringField(string name, string value, float width = 200)
        {
            GUILayout.BeginHorizontal();
            GUI.SetNextControlName(name); //指定下一控件用于控制聚焦的名字
            GUILayout.Label(name, GUILayout.Width(width * 0.3f));
            value = GUILayout.TextField(value, 200, GUILayout.Width(width * 0.7f));
            GUILayout.EndHorizontal();
            return value;
        }

        public static int DrawIntField(string name, int value, int width = 130)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label(name);
            value = EditorGUILayout.IntField(value, GUILayout.Width(width));
            GUILayout.EndHorizontal();
            return value;
        }

        public static float DrawFloatField(string name, float value, int width = 130)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label(name);
            value = EditorGUILayout.FloatField(value, GUILayout.Width(width));
            GUILayout.EndHorizontal();
            return value;
        }

        public static Object DrawVObjetField(string name, Type value, Object obj, int width = 130)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label(name);
            var objectField = EditorGUILayout.ObjectField(obj, value, false, GUILayout.Width(width));
            GUILayout.EndHorizontal();
            return objectField;
        }

        public static Object DrawHObjetField(string name, Type value, Object obj, float width = 130)
        {
            GUILayout.BeginHorizontal();
            GUILayout.Label(name, GUILayout.Width(width * 0.3f));
            var objectField = EditorGUILayout.ObjectField(obj, value, false, GUILayout.Width(width * 0.7f));
            GUILayout.EndHorizontal();
            return objectField;
        }

        public static string DrawStringField(Rect rect, string name, string value)
        {
            GUI.Label(new Rect(rect.x, rect.y, rect.width * 0.3f, rect.height), name);
            var result = GUI.TextField(
                new Rect(rect.x + rect.width * 0.3f, rect.y, rect.width * 0.7f, rect.height),
                value ?? string.Empty);
            return result;
        }

        public static int DrawIntField(Rect rect, string name, int value, float resize = 0.3f)
        {
            GUI.Label(new Rect(rect.x, rect.y, rect.width * resize, rect.height), name);
            var result = EditorGUI.IntField(
                new Rect(rect.x + rect.width * resize, rect.y, rect.width * (1 - resize), rect.height),
                value);
            return result;
        }

        public static Vector3 DrawVector3Field(Rect rect, string name, Vector3 value)
        {
            GUI.Label(new Rect(rect.x, rect.y, rect.width * 0.3f, rect.height), name);
            var result =
                EditorGUI.Vector3Field(new Rect(rect.x + rect.width * 0.3f, rect.y, rect.width * 0.7f, rect.height),
                    string.Empty, value);
            return result;
        }

        public static int DrawPopupField(Rect rect, string name, int selectId, GUIContent[] guiContent)
        {
            GUI.Label(new Rect(rect.x, rect.y, rect.width * 0.3f, rect.height), name);
            var result =
                EditorGUI.Popup(new Rect(rect.x + rect.width * 0.3f, rect.y, rect.width * 0.7f, rect.height),
                    selectId, guiContent);
            return result;
        }

        public static float DrawFloatField(Rect rect, string name, float value, float resize = 0.3f)
        {
            GUI.Label(new Rect(rect.x, rect.y, rect.width * resize, rect.height), name);
            var result = EditorGUI.FloatField(
                new Rect(rect.x + rect.width * resize, rect.y, rect.width * (1 - resize), rect.height),
                value);
            return result;
        }


        public static T DrawHObjetField<T>(Rect rect, string name, T editorGo) where T : Object
        {
            GUI.Label(new Rect(rect.x, rect.y, rect.width * 0.3f, rect.height), name);
            var result = EditorGUI.ObjectField(
                new Rect(rect.x + rect.width * 0.3f, rect.y, rect.width * 0.7f, rect.height),
                editorGo,
                typeof(T)) as T;
            return result;
        }


        public static void DrawBoxType1(Rect rect, Color colorBg, Color colorSp)
        {
            EditorGUI.DrawRect(rect, colorBg);
            EditorGUI.DrawRect(new Rect(rect.x, rect.yMax - 3, rect.width, 3), colorSp);
        }

        public static void DrawBoxType2(Rect rect, Color colorBg, Color colorSp)
        {
            EditorGUI.DrawRect(rect, colorBg);
            EditorGUI.DrawRect(new Rect(rect.x, rect.y, 2, rect.height), colorSp);
        }

        public static void DrawBoxType3(Rect rect, Color colorBg)
        {
            EditorGUI.DrawRect(new Rect(rect.x, rect.y, 2, rect.height), colorBg);
            EditorGUI.DrawRect(new Rect(rect.xMax - 2, rect.y, 2, rect.height), colorBg);
        }

        public static void DrawBoxType4(Rect rect, Color colorSp)
        {
            //sp
            EditorGUI.DrawRect(new Rect(rect.xMin, rect.yMin, rect.width, 2), colorSp);
            EditorGUI.DrawRect(new Rect(rect.xMin, rect.yMax, rect.width, 2), colorSp);

            EditorGUI.DrawRect(new Rect(rect.xMin, rect.y, 2, rect.height), colorSp);
            EditorGUI.DrawRect(new Rect(rect.xMax - 2, rect.y, 2, rect.height), colorSp);
        }
    }
}